#pragma once

#include <functional>
#include <sys/types.h>    
#include <sys/socket.h>
#include <errno.h>
#include <unistd.h>
#include <string.h>

#include "Logger.h"
#include "noncopyable.h"
#include "Socket.h"
#include "Channel.h"
#include "EventLoop.h"
#include "InetAddress.h"

class Acceptor : public noncopyable
{
public:
    using NewConnectionCallback = std::function<void(int sockfd, const InetAddress&)>;
    
    Acceptor(EventLoop* loop, const InetAddress& listenAddr, bool reusePort);
    ~Acceptor();

    void setNewConnectionCallback(const NewConnectionCallback& cb)
    {
        newConnectionCallback_ = cb;
    }

    bool listenning() const { return listenning_; }
    void listen();

private:
    void handleRead();
    
    EventLoop* loop_; // Acceptor用的就是用户定义的那个baseLoop，也称作mainLoop
    Socket acceptSocket_;
    Channel acceptChannel_;
    NewConnectionCallback newConnectionCallback_;
    bool listenning_;
};